package org.example.service.imp;

import org.example.dao.imp.ActivityDaoImp;
import org.example.entitys.managers.Activity;
import org.example.service.inter.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/11
 */

@Service
public class ActivityServiceImp implements ActivityService {
    @Autowired
    ActivityDaoImp activityDaoImp;

    @Override
    public boolean createActivity(Activity activity) {//活动必不可能重复
        return activityDaoImp.insert(activity) == 1;
    }

    @Override
    public boolean updateActivity(Activity activity) {
        if (activity.getId() == -1 || !activityDaoImp.isExitById(activity.getId())) return false;
        return activityDaoImp.update(activity) == 1;
    }

    @Override
    public boolean startActivity(int id) {
        if (!activityDaoImp.isExitById(id)) return false;//是否存在
        Activity activity = activityDaoImp.queryById(id);
        if (activity.getStatus() != 0) return false;//是否处于未启动状态
        activity.setStart_time(new Date());
        activity.setStatus(1);
        return activityDaoImp.update(activity) == 1;
    }

    @Override
    public boolean endActivity(int id) {
        if (!activityDaoImp.isExitById(id)) return false;//是否存在
        Activity activity = activityDaoImp.queryById(id);
        if (activity.getStatus() != 1) return false;//是否处于活动中状态
        activity.setEnd_time(new Date());
        activity.setStatus(2);
        return activityDaoImp.update(activity) == 1;
    }

    @Override
    public boolean archivedActivity(int id) {
        if (!activityDaoImp.isExitById(id)) return false;//是否存在
        Activity activity = activityDaoImp.queryById(id);
        if (activity.getStatus() != 2) return false;//是否处于结束状态
        activity.setArchived_time(new Date());
        activity.setStatus(3);
        return activityDaoImp.update(activity) == 1;
    }

    @Override
    public boolean deleteActivity(int id) {
        return activityDaoImp.delete(id) == 1;
    }

    @Override
    public Activity queryActivityById(int id) {
        if (activityDaoImp.isExitById(id)) return null;
        return activityDaoImp.queryById(id);
    }

    @Override
    public Activity[] queryActivityByNameAndStatus(String name, int status) {
        return activityDaoImp.queryActivityByNameAndStatus(name, status).toArray(new Activity[0]);
    }

    @Override
    public Activity[] queryActivityAll() {
        return activityDaoImp.queryAll().toArray(new Activity[0]);
    }

    @Override
    public Activity[] getActivityByStatus(int status) {
        if (status < 0 || status > 4) return null;
        return activityDaoImp.querySortedByStatus(status).toArray(new Activity[0]);
    }
}
